<template>
  <div class="smart-analysis">
    <n-card title="智能分析">
      <n-space vertical>
        <!-- 输入区域 -->
        <n-card title="输入内容">
          <n-input
            v-model:value="inputText"
            type="textarea"
            placeholder="请输入日志内容或问题描述..."
            :autosize="{ minRows: 5, maxRows: 10 }"
          />
          
          <!-- 示例操作日志（可拖拽） -->
          <div class="example-logs">
            <h4>示例操作日志（可拖拽到日志篮）</h4>
            <div class="log-examples">
              <div class="log-example" 
                   draggable="true"
                   @dragstart="handleLogDragStart($event, '2025-06-30 16:54:48 Redfish,Administrator@21.21.0.61,redfish,Delete event subscription successfully')"
                   title="拖拽到日志篮">
                <span class="log-time">2025-06-30 16:54:48</span>
                <span class="log-interface">Redfish</span>
                <span class="log-user">Administrator@21.21.0.61</span>
                <span class="log-component">redfish</span>
                <span class="log-message">Delete event subscription successfully</span>
                <span class="drag-hint">⋮⋮</span>
              </div>
              
              <div class="log-example" 
                   draggable="true"
                   @dragstart="handleLogDragStart($event, '2025-06-30 16:55:12 Redfish,Administrator@21.21.0.61,trust,Import CA server CA certificate failed')"
                   title="拖拽到日志篮">
                <span class="log-time">2025-06-30 16:55:12</span>
                <span class="log-interface">Redfish</span>
                <span class="log-user">Administrator@21.21.0.61</span>
                <span class="log-component">trust</span>
                <span class="log-message">Import CA server CA certificate failed</span>
                <span class="drag-hint">⋮⋮</span>
              </div>
            </div>
          </div>
        </n-card>

        <!-- 分析选项 -->
        <n-card title="分析选项">
          <n-space vertical>
            <n-form :model="analysisOptions" label-placement="left" label-width="120">
              <n-form-item label="选择用例">
                <n-select v-model:value="analysisOptions.caseId" :options="caseOptions" />
              </n-form-item>
              <n-form-item label="分析深度">
                <n-select v-model:value="analysisOptions.depth" :options="depthOptions" />
              </n-form-item>
            </n-form>
          </n-space>
        </n-card>

        <!-- 操作按钮 -->
        <n-space justify="center">
          <n-button 
            type="primary" 
            size="large"
            :loading="analyzing"
            :disabled="!canAnalyze"
            @click="handleAnalyze"
          >
            <template #icon>
              <n-icon><BulbOutline /></n-icon>
            </template>
            开始分析
          </n-button>
        </n-space>

        <!-- 分析结果 -->
        <n-card v-if="analysisResult" title="分析结果">
          <n-alert type="info" title="分析完成">
            基于您提供的内容，AI已生成详细的分析报告
          </n-alert>
          <n-space vertical style="margin-top: 16px;">
            <h3>{{ analysisResult.title }}</h3>
            <p>{{ analysisResult.summary }}</p>
          </n-space>
        </n-card>
      </n-space>
    </n-card>
  </div>
</template>

<script setup lang="ts">
import { ref, computed } from 'vue'
import { BulbOutline } from '@vicons/ionicons5'

// 响应式数据
const inputText = ref('')
const analyzing = ref(false)
const analysisResult = ref<{ title: string; summary: string } | null>(null)

// 分析选项
const analysisOptions = ref({
  caseId: '',
  depth: 'medium'
})

// 选项配置
const caseOptions = [
  { label: '自动选择', value: '' },
  { label: '错误日志分析', value: 'error' },
  { label: '性能监控分析', value: 'performance' },
  { label: '安全分析', value: 'security' }
]

const depthOptions = [
  { label: '快速分析', value: 'quick' },
  { label: '标准分析', value: 'medium' },
  { label: '深度分析', value: 'deep' }
]

// 计算属性
const canAnalyze = computed(() => {
  return inputText.value.trim().length > 0
})

// 方法
async function handleAnalyze() {
  analyzing.value = true
  
  try {
    // 模拟AI分析过程
    await new Promise(resolve => setTimeout(resolve, 2000))
    
    // 模拟分析结果
    analysisResult.value = {
      title: '发现潜在的性能问题',
      summary: '根据日志分析，系统存在响应时间过长的问题，可能影响用户体验。建议优化数据库连接池配置。'
    }
  } catch (error) {
    console.error('分析失败:', error)
  } finally {
    analyzing.value = false
  }
}

// 处理日志拖拽开始
function handleLogDragStart(event: DragEvent, rawLogContent: string) {
  if (!event.dataTransfer) return
  
  // 解析操作日志格式: 2025-06-30 16:54:48 Redfish,Administrator@21.21.0.61,redfish,Delete event subscription successfully
  const parts = rawLogContent.split(' ')
  
  if (parts.length >= 4) {
    const timestamp = `${parts[0]} ${parts[1]}`
    const interfacePart = parts[2] // Redfish,Administrator@21.21.0.61,redfish
    const messagePart = parts.slice(3).join(' ') // Delete event subscription successfully
    
    // 解析接口、用户、组件部分
    const interfaceParts = interfacePart.split(',')
    
    if (interfaceParts.length >= 3) {
      const interfaceName = interfaceParts[0] // Redfish
      const userInfo = interfaceParts[1] // Administrator@21.21.0.61
      const component = interfaceParts[2] // redfish
      
      // 构建拖拽数据
      const dragData = {
        rawContent: rawLogContent, // 完整的原始日志内容
        logType: 'operation',
        timestamp: timestamp,
        level: interfaceName, // Redfish作为日志级别
        userInfo: userInfo, // Administrator@21.21.0.61
        component: component, // redfish
        message: messagePart, // Delete event subscription successfully
        filePath: 'unknown',
        lineNumber: 0,
        logIndex: 0
      }
      
      // 设置拖拽数据
      event.dataTransfer.setData('application/json', JSON.stringify(dragData))
      event.dataTransfer.effectAllowed = 'copy'
      
      console.log('开始拖拽操作日志:', dragData)
    }
  }
}
</script>

<style scoped>
.smart-analysis {
  max-width: 1000px;
  margin: 0 auto;
}

.example-logs {
  margin-top: 16px;
  padding-top: 16px;
  border-top: 1px solid #e0e0e0;
}

.example-logs h4 {
  margin: 0 0 12px 0;
  color: #666;
  font-size: 14px;
}

.log-examples {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.log-example {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px;
  border: 1px solid #e0e0e0;
  border-radius: 6px;
  background-color: #fafafa;
  cursor: grab;
  transition: all 0.2s ease;
  user-select: none;
}

.log-example:hover {
  background-color: #f0f0f0;
  border-color: #d0d0d0;
}

.log-example:active {
  cursor: grabbing;
  transform: scale(0.98);
}

.log-time {
  min-width: 150px;
  font-family: monospace;
  color: #667eea;
  font-size: 12px;
}

.log-interface {
  padding: 4px 8px;
  background-color: #667eea;
  color: white;
  border-radius: 4px;
  font-size: 12px;
  font-weight: 500;
}

.log-user {
  min-width: 180px;
  color: #333;
  font-size: 12px;
}

.log-component {
  padding: 4px 8px;
  background-color: #f0f0f0;
  border-radius: 4px;
  font-size: 12px;
  color: #666;
}

.log-message {
  flex: 1;
  color: #333;
  font-size: 12px;
}

.drag-hint {
  color: #999;
  font-size: 16px;
  margin-left: auto;
}
</style> 